/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

Ext.onReady(function()
{
	if(true == AD.UI.map_bing_enabled)
	{
		var bing_zoom_level = 10;
		//bing_map_ctl.SetZoomLevel(zoomLevel);
		
		var bing_map_ctl = null;
		var pin_layer = null;
		var cur_data = null;
		
		var pin_lat_long = null;
		var pin_array = new Array();
		
		var cur_data = null;
	}
	
	
	function show_address_on_bing(layer, resultsArray, places, hasMore, veErrorMessage)
	{
		if(false == AD.UI.map_bing_enabled) return;
		
		if(null != veErrorMessage) return;
		
		pin_lat_long = places[0].LatLong;
		
		myShape = new VEShape(VEShapeType.Pushpin, pin_lat_long);

		VEPushpin.ShowDetailOnMouseOver = false;
		
		data = cur_data;
		address = data.street_number + ',' + data.street_name + ' ' + data.street_suffix +' ' + data.unit_number + '<br/>' + data.city + ', ' + data.state + ' ' + data.zip;
		status = AD.UI.fn_get_status(data);
		overlay_text = AD.UI.fn_get_map_overlay_text(data, address, status);
		
		myShape.SetTitle(data.ml_number);
		myShape.SetDescription(overlay_text);

		bing_map_ctl.ClearInfoBoxStyles();
		pin_layer.AddShape(myShape);

		bing_map_ctl.SetCenter(places[0].LatLong);
		bing_map_ctl.SetZoomLevel(bing_zoom_level);
		
	}

	
	AD.UI.fn_put_on_map_bing = function(data)
	{
		if(false == AD.UI.map_bing_enabled) return;
		
		cur_data = null;
		cur_data = data;
		var address = data.street_number + ',' + data.street_name + ' ' + data.street_suffix +' ' + data.unit_number + '<br/>' + data.city + ', ' + data.state + ' ' + data.zip;
		
		var status = AD.UI.fn_get_status(data);
		var overlay_text = AD.UI.fn_get_map_overlay_text(data, address, status);
		bing_map_ctl.ClearInfoBoxStyles();
		bing_map_ctl.Find(null, address, null, null, null, null,	true, true, null, true, show_address_on_bing);
	};
	

	AD.UI.map_bing_clear = function()
	{
		if(false == AD.UI.map_bing_enabled) return;
		
		pin_layer.DeleteAllShapes();
		bing_map_ctl.DeleteAllShapeLayers();
		bing_map_ctl.Clear();
		 pin_layer = null;
		
		 pin_layer = new VEShapeLayer();
		 pin_layer.SetTitle('pin_layer');
		  
		 bing_map_ctl.AddShapeLayer(pin_layer);
	};
	
	function ShapeInfo(e){
		if(null != e.elementID)
		{
		   var shape = null;
		   var count = pin_layer.GetShapeCount();
	       for(i=0; i<count; i++)
	       {
	    	   shape = pin_layer.GetShapeByIndex(i);
	    	   str = e.elementID.substring(0, shape.guid.length);
	    	   
	    	   if(str == shape.guid)
	    	   {
	    		   bing_map_ctl.ShowInfoBox(shape);
	    	   }
	       }
		}
	}
	  function get_bing_map() {
		  bing_map_ctl = new VEMap('myMap:newmap');
		  bing_map_ctl.LoadMap();
		  bing_map_ctl.ClearInfoBoxStyles();

		  pin_layer = new VEShapeLayer();
		  pin_layer.SetTitle('pin_layer');
		  
		  bing_map_ctl.AddShapeLayer(pin_layer);
		  
		  bing_map_ctl.AttachEvent("onclick", ShapeInfo);		  
	  }
	  
	if(true == AD.UI.map_bing_enabled) 
	{
	
	  AD.UI.map_bing = new Ext.Window({
		  id:'map_bing-win',
		  width: AD.Globals.client_width * 0.30,
		  height: AD.Globals.client_height * 0.60,
		  autoScroll:true,
		  collapsible : true,
		  collapsed : true,
		  closable : false,
		  frame : true,
		  layout:'fit',		  
		  minimizable: false,
		  maximizable: true,
		  stateful :true,
		  stateId:'map_bing_win',
		  title : 'Bing Maps',
		  items : [
		           { xtype: 'box', autoEl: { tag: 'div',html: "<div id='myMap:newmap' 	style='width: 100%; height:  600px;'>BingMaps...</div>"}}]
		  });

	  //Show the window..
	  AD.UI.map_bing.show();
	  
	//Load the map inside the window
	  get_bing_map();
	}
});
